#include "dyelibraryui.h"
#include "ui_dyelibraryui.h"

#include <QDebug>
#include <QMessageBox>

TargetWidget::TargetWidget(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::TargetWidget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowCloseButtonHint);

    cusMsg = new CustomMessageBox();
    addNDye = new AddNewDye();
    DLDB = new DyeLibrary();
    configFileService = new ConfigFileService();

    /* 自适应宽度 */
    ui->dyeTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    /* 渲染默认染料 */
    allDyeInfos = configFileService->getDyes();
    this->renderNewDyeInfo(allDyeInfos, true);

    /* 渲染非默认染料 */
    allDyeInfos = DLDB->show();
    this->renderNewDyeInfo(allDyeInfos, false);

    /* 初始化 */
    ui->dyeTable->hideColumn(5);
    ui->filterItem_dyeCreator->hide();
    ui->filterItem_dyeType->hide();

    connect(ui->newDye, &QPushButton::clicked, this, [=](){
        addNDye->exec();
    });
    connect(addNDye, &AddNewDye::newDyeInfo, this, &TargetWidget::addNewDyeLibInfoToDB);
    connect(ui->filterKey, SIGNAL(currentIndexChanged(int)), this, SLOT(changeFilterCondition(int)));
    connect(ui->filter, &QPushButton::clicked, this, &TargetWidget::filterTargetTable);
    connect(ui->reset, &QPushButton::clicked, this, [=](){
        ui->filterItem_nameAndWave->clear();
        for(int i = 0; i < ui->dyeTable->rowCount(); i++)
            ui->dyeTable->showRow(i);
    });
    connect(ui->dyeTable, &QTableWidget::cellChanged, this, &TargetWidget::reviseDyeTable);
    connect(ui->ok, &QPushButton::clicked, this, [=]{
        emit TargetWidget::okExport();
        this->close();
    });

}

TargetWidget::~TargetWidget()
{
    delete ui;
}

/* 改变过滤体哦阿健 */
void TargetWidget::changeFilterCondition(int index){
    this->filterType = index;
    switch (index) {
    case 0:
    case 3:
        ui->filterItem_dyeCreator->hide();
        ui->filterItem_dyeType->hide();
        ui->filterItem_nameAndWave->show();
        break;
    case 1:
        ui->filterItem_dyeCreator->show();
        ui->filterItem_dyeType->hide();
        ui->filterItem_nameAndWave->hide();
        break;
    case 2:
        ui->filterItem_dyeCreator->hide();
        ui->filterItem_dyeType->show();
        ui->filterItem_nameAndWave->hide();
        break;
    }
}

void TargetWidget::renderNewDyeInfo(QList<QHash<QString, QString>> allDyeInfos, bool isDefault){
    ui->dyeTable->blockSignals(true);    // 阻塞信号

    for(int i = 0; i < allDyeInfos.size(); i++){
        QHash<QString, QString> dyeInfo = allDyeInfos[i];

        QPushButton *deleteDye = new QPushButton();
//        deleteDye->setIcon(QIcon(":/icon/icon/delete_btn.svg"));
//        deleteDye->setIconSize(QSize(35, 35));
        deleteDye->setStyleSheet("background: transparent; border: none; font-family: 'Alibaba PuHuiTi'; color: #000");
        deleteDye->setText("删除");
        deleteDye->setCursor(Qt::PointingHandCursor);

        /* 数据渲染 */
        int row = ui->dyeTable->rowCount();
        ui->dyeTable->setRowCount(row+1);
        ui->dyeTable->setItem(row, 0, new QTableWidgetItem(dyeInfo["name"]));
        ui->dyeTable->setItem(row, 1, new QTableWidgetItem(dyeInfo["creator"]));
        ui->dyeTable->setItem(row, 2, new QTableWidgetItem(dyeInfo["type"]));
        ui->dyeTable->setItem(row, 3, new QTableWidgetItem(dyeInfo["wave_length"]));
        ui->dyeTable->setCellWidget(row, 4, deleteDye);
        ui->dyeTable->setItem(row, 5, new QTableWidgetItem(dyeInfo["id"]));

        connect(deleteDye, &QPushButton::clicked, this, [=](){
            QString queryText = "确认删除染料 \"" + dyeInfo["name"] + "\" ?";
            if(QMessageBox::Yes == cusMsg->show(this, CMsg::CQuestion, "确认删除", queryText)){
                DLDB->remove(dyeInfo["id"]);
                /* 初始化 */
                ui->dyeTable->setRowCount(0);
                /* 渲染默认染料 */
                this->renderNewDyeInfo(configFileService->getDyes(), true);
                // 调用show方法
                QList<QHash<QString,QString>> allDyeInfos = DLDB->show();
                this->renderNewDyeInfo(allDyeInfos, false);
            }
        });

        /* 默认染料，删除按钮失效，行内不可编辑 */
        if(isDefault){
            deleteDye->setEnabled(false);
            ui->dyeTable->item(row, 0)->setFlags(ui->dyeTable->item(row, 0)->flags() & (~Qt::ItemIsEditable));
            ui->dyeTable->item(row, 1)->setFlags(ui->dyeTable->item(row, 1)->flags() & (~Qt::ItemIsEditable));
            ui->dyeTable->item(row, 2)->setFlags(ui->dyeTable->item(row, 2)->flags() & (~Qt::ItemIsEditable));
            ui->dyeTable->item(row, 3)->setFlags(ui->dyeTable->item(row, 3)->flags() & (~Qt::ItemIsEditable));
        }
    }
    ui->dyeTable->blockSignals(false);    // 释放信号
}

void TargetWidget::filterTargetTable(){
    for(int i = 0; i < ui->dyeTable->rowCount(); i++) ui->dyeTable->showRow(i);
    if(ui->filterCondition->currentIndex() == 0){
        switch (this->filterType) {
        case 0:
            for(int i = 0; i < ui->dyeTable->rowCount(); i++) {
                if(ui->filterItem_nameAndWave->text() != ui->dyeTable->item(i, 0)->text())
                    ui->dyeTable->hideRow(i);
            }
            break;
        case 1:
            for(int i = 0; i < ui->dyeTable->rowCount(); i++) {
                if(ui->filterItem_dyeCreator->currentText() != ui->dyeTable->item(i, 1)->text())
                    ui->dyeTable->hideRow(i);
            }
            break;
        case 2:
            for(int i = 0; i < ui->dyeTable->rowCount(); i++) {
                if(ui->filterItem_dyeType->currentText() != ui->dyeTable->item(i, 2)->text())
                    ui->dyeTable->hideRow(i);
            }
            break;
        case 3:
            for(int i = 0; i < ui->dyeTable->rowCount(); i++) {
                if(ui->filterItem_nameAndWave->text() != ui->dyeTable->item(i, 3)->text())
                    ui->dyeTable->hideRow(i);
            }
            break;
        }
    }
    else {
        switch (this->filterType) {
        case 0:
            for(int i = 0; i < ui->dyeTable->rowCount(); i++) {
                if(ui->filterItem_nameAndWave->text() == ui->dyeTable->item(i, 0)->text())
                    ui->dyeTable->hideRow(i);
            }
            break;
        case 1:
            for(int i = 0; i < ui->dyeTable->rowCount(); i++) {
                if(ui->filterItem_dyeCreator->currentText() == ui->dyeTable->item(i, 1)->text())
                    ui->dyeTable->hideRow(i);
            }
            break;
        case 2:
            for(int i = 0; i < ui->dyeTable->rowCount(); i++) {
                if(ui->filterItem_dyeType->currentText() == ui->dyeTable->item(i, 2)->text())
                    ui->dyeTable->hideRow(i);
            }
            break;
        case 3:
            for(int i = 0; i < ui->dyeTable->rowCount(); i++) {
                if(ui->filterItem_nameAndWave->text() == ui->dyeTable->item(i, 3)->text())
                    ui->dyeTable->hideRow(i);
            }
            break;
        }
    }
}

void TargetWidget::addNewDyeLibInfoToDB(AddNewDye::DyeStruct newDyeInfo){
    DLDB->create(newDyeInfo.dyeName,
                 newDyeInfo.waveLength,
                 newDyeInfo.dyeCreator,
                 newDyeInfo.dyeType,
                 newDyeInfo.status
                 );
    /* 初始化 */
    ui->dyeTable->setRowCount(0);
    /* 渲染默认染料 */
    this->renderNewDyeInfo(configFileService->getDyes(), true);
    /* 调用show方法，获取所有染料信息 */
    QList<QHash<QString, QString>> allDyeInfos = DLDB->show();
    renderNewDyeInfo(allDyeInfos, false);
}

void TargetWidget::reviseDyeTable(int row, int column){
    DLDB->revise(
                ui->dyeTable->item(row, 5)->text(),
                ui->dyeTable->item(row, 0)->text(),
                ui->dyeTable->item(row, 3)->text(),
                ui->dyeTable->item(row, 2)->text(),
                ui->dyeTable->item(row, 2)->text(),
                "1"
                );
    /* 初始化 */
    ui->dyeTable->setRowCount(0);
    /* 渲染默认染料 */
    this->renderNewDyeInfo(configFileService->getDyes(), true);
    /* 调用show方法，获取所有染料信息 */
    QList<QHash<QString, QString>> allDyeInfos = DLDB->show();
    renderNewDyeInfo(allDyeInfos, false);
}
